/**
 * 
 */
package com.ffcs.crmd.tsp.server.netty.processor;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ffcs.crmd.tsp.api.dto.client.TspJobDetail;
import com.ffcs.crmd.tsp.api.dto.netty.RemotingCommand;
import com.ffcs.crmd.tsp.api.dto.page.PageInfo;
import com.ffcs.crmd.tsp.api.dto.page.RetVo;
import com.ffcs.crmd.tsp.api.netty.INettyProcessor;
import com.ffcs.crmd.tsp.common.constant.LoggerName;
import com.ffcs.crmd.tsp.common.netty.ResponseCode;
import com.ffcs.crmd.tsp.core.util.BeanUtils;
import com.ffcs.crmd.tsp.core.util.XmlUtil;
import com.ffcs.crmd.tsp.server.core.DataBaseHandler;
import com.ffcs.crmd.tsp.server.core.RemotingHandler;
import com.ffcs.crmd.tsp.server.jdbc.entity.JobDetail;
import com.ffcs.crmd.tsp.server.util.JobDetailHelper;

import io.netty.channel.ChannelHandlerContext;

/**
 * 功能说明:
 *
 * @author ZHONGFUHUA
 * 
 * @since 
 *
 */
public class QueryJobDetailProcessor implements INettyProcessor {
    
    /**
     * 日志
     */
    private final static Logger LOG = LoggerFactory.getLogger(LoggerName.RemotingLoggerName);
    
    /**
     * 任务管理控制中心
     */
    private DataBaseHandler     dataBaseHandler;
    
    /**
     * 功能说明:
     * 
     * @param remotingController
     */
    public QueryJobDetailProcessor(RemotingHandler remotingController, DataBaseHandler dataBaseHandler) {
        this.dataBaseHandler = dataBaseHandler;
    }
    
    /**
     * 功能说明:
     * 
     * @param ctx
     * @param request
     * @return
     */
    @Override
    public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) {
        RemotingCommand response = RemotingCommand.createResponseCommand(ResponseCode.SUCCESS);
        String requestXml = new String(request.getBody());
        LOG.debug("SERVER-NETTY: one request to query job detail is received that body=[{}]", requestXml);
        try {
            TspJobDetail tspJobDetailVO = (TspJobDetail) XmlUtil.fromXml(requestXml, TspJobDetail.class);
            RetVo<TspJobDetail> tspJobDetailRetVo = new RetVo<TspJobDetail>();
            Integer pageNumber = tspJobDetailVO.getPageNumber();
            Integer pageSize = tspJobDetailVO.getPageSize();
            
            JobDetail jobDetailParam = new JobDetail();
            BeanUtils.applyIf(jobDetailParam, tspJobDetailVO);
            
            if (pageNumber != null && pageSize != null) {
                PageInfo<JobDetail> jobDetailPageInfo = dataBaseHandler.getServiceSupport().queryJobDetailPage(jobDetailParam);
                PageInfo<TspJobDetail> tspJobDetailPageInfo = new PageInfo<TspJobDetail>();
                List<TspJobDetail> tspJobDetailVOList = convert(jobDetailPageInfo.getList());
                BeanUtils.applyIf(tspJobDetailPageInfo, jobDetailPageInfo);
                tspJobDetailPageInfo.setList(tspJobDetailVOList);
                tspJobDetailRetVo.setPageInfo(tspJobDetailPageInfo);
            } else if (tspJobDetailVO.getJobId() != null) {
                TspJobDetail tspJobDetail = new TspJobDetail();
                JobDetail jobDetail = dataBaseHandler.getServiceSupport().getJobDetail(jobDetailParam);
                BeanUtils.applyIf(tspJobDetail, jobDetail);
                tspJobDetailRetVo.setObject(tspJobDetail);
            } else {
                List<JobDetail> jobDetailList = dataBaseHandler.getServiceSupport().queryJobDetail(jobDetailParam);
                List<TspJobDetail> tspJobDetailVOList = convert(jobDetailList);
                tspJobDetailRetVo.setDataList(tspJobDetailVOList);
            }
            response.setBody(XmlUtil.toXml(tspJobDetailRetVo).getBytes());
        } catch (Exception e) {
            LOG.error("SERVER-NETTY: query job detail[{}] exception:", requestXml, e);
            String responseBody = String.format("query job detail:%s", e.getMessage());
            response.setCode(ResponseCode.FAIL);
            response.setBody(responseBody.getBytes());
        }
        return response;
    }
    
    private List<TspJobDetail> convert(List<JobDetail> jobDetailList) throws Exception {
        List<TspJobDetail> tspJobDetailVOList = new ArrayList<TspJobDetail>();
        if (CollectionUtils.isNotEmpty(jobDetailList)) {
            for (JobDetail jobDetail : jobDetailList) {
                TspJobDetail tspJobDetail = new TspJobDetail();
                BeanUtils.applyIf(tspJobDetail, jobDetail);
                JobDetailHelper.convert2JobDetailVO(tspJobDetail, jobDetail);
                tspJobDetailVOList.add(tspJobDetail);
            }
        }
        return tspJobDetailVOList;
    }
    
}
